package com.epin.config.aop;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.stereotype.Component;

import com.epin.util.SessionUtil;

@Aspect
@Component
public class CommonDataAop {
	
    private Logger logger = LoggerFactory.getLogger(CommonDataAop.class);
    
    private static final String CREATOR_ID     = "creatorId";
    private static final String CREATE_TIME    = "createTime";
    private static final String UPDATOR_ID   = "updatorId";
    private static final String UPDATE_TIME = "updateTime";


    @Pointcut(value="execution(* com.baomidou.mybatisplus.service.IService.insert*(..))")
    private void insert() {
    	
    }
    
    @Pointcut(value="execution(* com.baomidou.mybatisplus.service.IService.update*(..))")
    private void update() {
    	
    }

    @Before("insert()")
    public void doInsertBefore(JoinPoint jp) {
    	Object[] args = jp.getArgs();

        if (args != null && args.length > 0) {
            Object argument = args[0];
            BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
            // 设置创建时间和修改时间
            if (beanWrapper.isWritableProperty(CREATE_TIME) && beanWrapper.getPropertyValue(CREATE_TIME)==null && beanWrapper.getPropertyValue("id")==null) {
                beanWrapper.setPropertyValue(CREATE_TIME, new Date());
            }
            if (beanWrapper.isWritableProperty(UPDATE_TIME)) {
                beanWrapper.setPropertyValue(UPDATE_TIME, new Date());
            }
            // 设置创建人
            if (beanWrapper.isWritableProperty(CREATOR_ID)  && beanWrapper.getPropertyValue(CREATOR_ID)==null && beanWrapper.getPropertyValue("id")==null){
            	Integer userId = SessionUtil.getloginUserId();
            	if (userId != null){
            		beanWrapper.setPropertyValue(CREATOR_ID, userId);
            	}
            }
            // 设置修改人
            if (beanWrapper.isWritableProperty(UPDATOR_ID)){
            	Integer userId = SessionUtil.getloginUserId();
            	if (userId != null){
            		beanWrapper.setPropertyValue(UPDATOR_ID, userId);
            	}
            }
        }
        
    }
    
    @Before("update()")
    public void doUpdateBefore(JoinPoint jp) {
    	Object[] args = jp.getArgs();
    	
    	if (args != null && args.length > 0) {
    		Object argument = args[0];
    		BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
    		// 设置修改时间
    		if (beanWrapper.isWritableProperty(UPDATE_TIME)) {
    			beanWrapper.setPropertyValue(UPDATE_TIME, new Date());
    		}
    		// 设置创建人和修改人
    		if (beanWrapper.isWritableProperty(UPDATOR_ID)){
    			Integer userId = SessionUtil.getloginUserId();
    			if (userId != null){
    				beanWrapper.setPropertyValue(UPDATOR_ID, userId);
    			}
    		}
    	}
    }

}
